﻿/*******************************************************************************
* 
* Entry for Packaging tool
* 
* 
******************************************************************************/
#include "../common/common.h"
#include <windows.h>

using namespace std;
#define FE_PE_NAME L"fe.exe"

int main(int argc, char *argv[])
{
    LOG_DEBUG("Ready to package resources.");

    for(int i=0; i<argc; i++)
    {
        LOG_DEBUG("argv[%d]=%s", i, argv[i]);
    }

    if(argc != 3)
    {
        LOG_ERROR("argument dismatch. usage: package [source] [dest]");
        return 0;
    }

    HMODULE hMod;
    // determine the process name
    auto name = Process::getBaseName();

    if(name == FE_PE_NAME)
    {
        hMod = NULL;
    }
    else
    {
        hMod = LoadLibraryEx(FE_PE_NAME, NULL, LOAD_LIBRARY_AS_DATAFILE);
    }

    if(!hMod)
    {
        LOG_FATAL("Failure initializing ResourceManager: Failed to load module.");
        return -1;
    }

    ResourceManager res(hMod);

    auto source = Strings::a2w(argv[1]);
    auto dest = Strings::a2w(argv[2]);
    res.package(source, dest);


    // unload library if needed
    if(name != FE_PE_NAME)
    {
        FreeLibrary(hMod);
    }

    LOG_DEBUG("Packaging finished.");
    return 0;
}